<?php
namespace Admin\Controller;
class NewsController extends IsloginController{
	function add(){
		$new = D("Xinwenlei");
		$arr = $new->select();
		$this->assign('arr',$arr);
		$this->show();
	}
	function fabu(){
		$upload= new \Think\Upload();
		$upload->autoSub  =false;
		$upload->mimes=array('image/png',"image/gif","image/jpeg");
		$upload->rootPath = './Public';
       	$upload->savePath = '/Upload/';
		$info=$upload->upload();
		if($info){
			$_POST['imagename']=$info['imagename']['savename'];
		}
		$re=M("News");
		$_POST["addtime"]=time();
		$arr1 = $re->data($_POST)->add();
		if($arr1){
		    $this->success('添加成功！',U('News/oper'));
        }else{
		    $this->error("添加失败",U('News/add'));
        }
	}
	function del(){
		$del = M("News");
		$id=$_POST["id"];
		$re = $del->where("id=$id")->delete();
		if($re){
			$this->success("删除成功",U("News/oper"));
		}else{
			$this->error("删除失败",U("News/oper"));
		}
	}
	function oper(){
        if(isset($_SERVER["HTTP_X_REQUESTED_WITH"]) && strtolower($_SERVER["HTTP_X_REQUESTED_WITH"])=="xmlhttprequest") {
            $this->seachs();
        }else{
		$op = M("News");
		$num=$op->count();
// 		3、规定每页显示的条数
		$pageSize=8;
// 		4、实例化分页类
		$pageOb=new \Think\Page($num,$pageSize);
// 		5、获取开始位置值
		$start=$pageOb->firstRow;
// 		6、获取分页结果
		$pageStr=$pageOb->show();
		$arr = $op->limit("$start,$pageSize")->select();
            $this->assign('pageStr',$pageStr);
        }
        $this->assign('arr',$arr);
		$this->display();
	}
	function update(){
		$up = M("News");
		$id=$_GET["id"];
		$arr = $up->where("id=$id")->find();	
		$fenlei = M("Xinwenlei");
		$arr1 = $fenlei->select();
		$this->assign('arr',$arr);
		$this->assign('arr1',$arr1);
		$this->display();
	}
	function save(){
		$new = M("News");
		$_POST['addtime']=time();
		$arr = $new->data($_POST)->save();
		if($arr){
			echo "YES";
		}else{
			echo "no";
		}
	}
	function seachs(){
        if(isset($_SERVER["HTTP_X_REQUESTED_WITH"]) && strtolower($_SERVER["HTTP_X_REQUESTED_WITH"])=="xmlhttprequest") {
            $newsOb = M("News");
            $con = $_POST["keyword"];
            $map['id|title|daoyu|status']=array('like', "%$con%");
            $arr = $newsOb->where($map)->select();
            echo json_encode($arr);
        }
    }
}